package cn.xdf.controller;

import cn.xdf.entity.City;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author lscl
 * @version 1.0
 * @intro: @ModelAttribute注解作用与方法上
 */
@Controller
@RequestMapping("/demo09")
public class Demo09Controller_ModelAttribute02 {
    /**
     * ModelAttribute标注的方法在所有的handler方法之前执行
     *
     * @param maps
     * @throws IOException
     */
    @ModelAttribute
    public void model(Map<String, Object> maps) throws IOException {

        maps.put("city", new City(1, "南宁", 4700.00, true));
        System.out.println("model方法执行了...");
    }

    // 如果前端没有传递则以@ModelAttribute标注的为准,如果传递了则以前端参数为准
    @RequestMapping("/demo01")
    public void demo01(@ModelAttribute("city") City city, HttpServletResponse response) throws Exception{

        response.setContentType("text/html;charset=utf8");
        response.getWriter().println(city);
    }
}